<template>
  <el-dialog :title="title" :visible.sync="isShow" @close="closeEvent">
    <el-form ref="form" label-width="100px" :model="form" :rules="rules">
      <el-form-item label="权限名称" prop="name">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="权限标识" prop="code">
        <el-input v-model="form.code" />
      </el-form-item>
      <el-form-item label="权限描述" prop="description">
        <el-input v-model="form.description" />
      </el-form-item>
      <el-form-item label="企业可见" prop="enVisible">
        <el-switch
          v-model="form.enVisible"
          active-text="可见"
          inactive-text="不可见"
          inactive-value="0"
          active-value="1"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <div style="text-align: center">
        <el-button type="primary" @click="submmit">确定</el-button>
        <el-button @click="isShow = false">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import { sysPermissionPost, sysPermissionPut } from '@/api/permisse'
export default {
  data() {
    return {
      mode: '',
      isShow: false,
      bol: false,
      form: {
        enVisible: '0', //	string	非必须
        name: '', //	string	非必须
        code: '', //	string	非必须
        description: '', //	string	非必须
        type: '', //	number	非必须
        pid: '' //	string	非必须
      },
      rules: {
        enVisible: [{ required: true, message: '必填', trigger: 'change' }],
        name: [{ required: true, message: '必填', trigger: 'change' }],
        code: [{ required: true, message: '必填', trigger: 'change' }],
        description: [{ required: true, message: '必填', trigger: 'change' }]
      }
    }
  },
  computed: {
    title() {
      return this.mode === 'add' ? '新增权限点' : '编辑权限点'
    }
  },
  methods: {
    // 点击提交
    submmit() {
      // 验证为true
      this.$refs.form.validate(async(results) => {
        //  发起添加请求
        if (results) {
          if (this.mode === 'add') {
            const res = await sysPermissionPost(this.form)
            console.log(res)
            // 提示成功
            this.$message.success('添加成功')
          } else if (this.mode === 'edit') {
            await sysPermissionPut(this.form)
            // 提示成功
            this.$message.success('修改成功')
          }
          // 关闭弹框
          this.isShow = false
          // 刷新父级列表
          this.$emit('getData')
        }
      })
    },
    closeEvent() {
      this.$refs.form.resetFields()
      this.form = {
        enVisible: '', //	string	非必须
        name: '', //	string	非必须
        code: '', //	string	非必须
        description: '', //	string	非必须
        type: '', //	number	非必须
        pid: '' //	string	非必须
      }
    }
  }
}
</script>

<style></style>
